package managedbean.FRM.CashInAdvance;

import entity.CRM.SaleOrder;
import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.FRM.CustomerInvoiceBeanLocal;


@ManagedBean(name="addInvoiceCustomer_CIAManagerBean")
@ViewScoped
public class AddInvoiceCustomer_CIAManagerBean implements Serializable {
    public AddInvoiceCustomer_CIAManagerBean() {}
    
    @EJB
    private CustomerInvoiceBeanLocal customerInvoiceBeanLocal;
    private SaleOrder selectedSaleOrder;
    private Double cashInAdvance;
    private String statusMessage;
    
    public void initView(PhaseEvent event){
        try
        {
            if(selectedSaleOrder==null)
            {
                selectedSaleOrder = (SaleOrder)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedSaleOrder");
                cashInAdvance = (Double) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("cashInAdvance");                
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();            
        }
    }
    
    public SaleOrder getSelectedSaleOrder() {
        return selectedSaleOrder;
    }

    public void setSelectedSaleOrder(SaleOrder selectedSaleOrder) {
        this.selectedSaleOrder = selectedSaleOrder;
    }

    public Double getCashInAdvance() {
        return cashInAdvance;
    }

    public void setCashInAdvance(Double cashInAdvance) {
        this.cashInAdvance = cashInAdvance;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public void makeCashInAdvance(ActionEvent event) throws IOException
    {
        // This part is incorrect: it must create an invoice instead of go straight into balance
        //cashInAdvanceBeanLocal.createCashInAdvance(selectedSaleOrder.getId(), cashInAdvance);
        //statusMessage="New cash of $"+this.cashInAdvance+ " for the order with ID "+selectedSaleOrder.getId()+" has been made in advance successfully.";
        //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Result: " +statusMessage + ". Your credit balance has been updated to $" +customerBeanLocal.findCustomerById(selectedCustomer.getRegistrationNumber()).getCreditAccount().getCreditBalance(), "")); 
        //this.setSelectedCustomer(customerBeanLocal.findCustomerById(selectedCustomer.getRegistrationNumber()));
        selectedSaleOrder = this.getSelectedSaleOrder();
        cashInAdvance = this.getCashInAdvance();
        
        Long customerInvoiceId=customerInvoiceBeanLocal.createCustomerInvoice_CashInAdvance(selectedSaleOrder.getId(), cashInAdvance);
        statusMessage="New invoice of $"+cashInAdvance+" created succesfully." ;
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + statusMessage, ""));
            
    }
    
}
